use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashSet;

pub struct ScriptInfo<'a> {
    pub start_index: usize,
    pub end_index: usize,
    pub inner_start_index: usize,
    pub inner_end_index: usize,
    pub origin_content: &'a str,
}

impl<'a> ScriptInfo<'a> {
    pub fn events(&self) -> Vec<String> {
        lazy_static! {
            static ref RE: Regex =
                Regex::new(r#"\$emit\("([a-zA-Z0-9_]+)""#).expect("script info regex error");
        }
        let captures =
            RE.captures_iter(&self.origin_content[self.inner_start_index..self.inner_end_index]);

        let mut event_list: Vec<String> = vec![];
        println!("line");
        for capture in captures {
            event_list.push(capture[1].to_string());
        }

        let set: HashSet<String> = event_list.into_iter().collect();
        let result: Vec<String> = set.into_iter().collect();
        result
    }
}
